/*
 * indexation.cpp
 *
 *  Created on: 14 nov. 2011
 *      Author: arthur
 */
#include "indexation.h"

int minimum(int a, int b, int c)
{
	if (a <= b && a <= c)
	{
		return a;
	}
	else if (b <= c)
	{
		return b;
	}
	else
	{
		return c;
	}
}

unsigned int DistanceLevensthein(string mot1, string mot2)
{
	int ** matrice = (int **) malloc((mot1.size() + 1) * sizeof(int *));
	int i, j;

	for (i = 0; i <= (int) mot1.size(); i++)
	{
		matrice[i] = (int *) malloc((mot2.size() + 1) * sizeof(int));
		matrice[i][0] = i;
	}
	for (j = 0; j <= (int) mot2.size(); j++)
	{
		matrice[0][j] = j;
	}

	for (i = 1; i <= (int) mot1.size(); i++)
	{
		for (j = 1; j <= (int) mot2.size(); j++)
		{
			if (mot1[i - 1] == mot2[j - 1])
			{
				matrice[i][j] = matrice[i - 1][j - 1];
			}
			else
			{
				matrice[i][j] = minimum(matrice[i][j - 1] + 1, matrice[i - 1][j] + 1,
						matrice[i - 1][j - 1] + 1);
			}

		}
	}
	return matrice[mot1.size()][mot2.size()];
}

unsigned int DistanceLevensthein(Image image1, Image image2)
{
	return DistanceLevensthein(image1.motClef, image2.motClef);
}
